package com.csun.cmny.provider.device.patrol;

import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.netty.NettyInitRunner;
import io.netty.bootstrap.ServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/7/22 13:35
 * @Description: 巡更棒服务器开关配置
 */
@Slf4j
@Order(35)
@Configuration
@ConditionalOnProperty(prefix = "cmny.netty.patrol", name = "switch", havingValue = "on")
public class PatrolNettyConfig {

    @Resource
    private CmnyProperties cmnyProperties;
    @Resource
    private PatrolNettyTcpChannelHandler patrolNettyTcpChannelHandler;

    @Bean
    public PatrolNettyTcpServer patrolNettyTcpServer() {

        int port = cmnyProperties.getNetty().getPatrol().getPort();

        log.info("### patrol init runner starting...<<<<<<<<<<<<<");

        PatrolNettyTcpServer patrolNettyTcpServer =
                new PatrolNettyTcpServer(port, patrolNettyTcpChannelHandler, new ServerBootstrap());

        patrolNettyTcpServer.init();

        NettyInitRunner.nettyServerMap.put(port, patrolNettyTcpServer);

        log.info("### patrol init runner finish...<<<<<<<<<<<<<");

        return patrolNettyTcpServer;
    }
}
